package com.xxxx.server.controller;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.deploy.net.HttpUtils;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IIvaccineService;
import com.xxxx.server.service.ISdakaService;
import com.xxxx.server.service.ISvaccineService;
import com.xxxx.server.service.ITvaccineService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/Statistics/")
public class StatisticsController {
    @Autowired
    private IIvaccineService iIvaccineService;
    @Autowired
    private ITvaccineService iTvaccineService;
    @Autowired
    private ISvaccineService iSvaccineService;

    @Autowired
    private ISdakaService iSdakaService;

    @ApiOperation(value = "全体疫苗接种情况")
    @GetMapping("/getallIvaccines")
    public List<Integer> getAllIvaccines(){
        List  temp = new ArrayList<>();
        //全程接种 type =1;
        temp.add(findIvaccines(1));
        //部分接种 type =2；
        temp.add(findIvaccines(2));
        //未接种 type =3；
        temp.add(findIvaccines(3));
        return temp;
    }

    private Integer findIvaccines(int type){
        Integer count1=0;
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IVtype",type);
        queryWrapper.eq("enable",1);
        count1=iIvaccineService.count(queryWrapper);

        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("SVtype",type);
        queryWrapper2.eq("enable",1);
        count1+=iSvaccineService.count(queryWrapper2);

        QueryWrapper queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("TVtype",type);
        queryWrapper3.eq("enable",1);
        count1+=iTvaccineService.count(queryWrapper3);
        return count1;
    }

    @ApiOperation(value = "个人疫苗接种情况")
    @GetMapping("/getOneIvaccines")
    public List<String> getOneIvaccines(){
        List temp = new ArrayList<>();
        int roleId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getRoles().get(0).getId();
        int adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable",1);
        if(roleId == 5){
            queryWrapper.eq("SVid",adminId);
            temp.add(iSvaccineService.getOne(queryWrapper).getSVtype());
            temp.add(iSvaccineService.getOne(queryWrapper).getSVprogress());
        }else if(roleId == 4){
            queryWrapper.eq("TVid",adminId);
            temp.add(iTvaccineService.getOne(queryWrapper).getTVtype());
            temp.add(iTvaccineService.getOne(queryWrapper).getTVprogress());
        }else if(roleId == 3){
            queryWrapper.eq("IVid",adminId);
            temp.add(iIvaccineService.getOne(queryWrapper).getIVtype());
            temp.add(iIvaccineService.getOne(queryWrapper).getIVprogress());
        }else{
            temp.add(0);
            temp.add(0);
        }
        return temp;
    }


    @ApiOperation(value = "学院-打卡人数情况")
    @GetMapping("/getalldakaByCo")
    public List<Map<Object,Object>> getalldakaByCo(){
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = sf.format(now);
        String sdate = nowDay + ":00:00:00";
        String edate = nowDay + ":23:59:59";

       return iSdakaService.getAllDaka(sdate,edate);
    }

    @ApiOperation(value = "班级-打卡人数情况")
    @GetMapping("/getalldakaByCl")
    public List<Map<Object,Object>> getalldakaByCl(){
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = sf.format(now);
        String sdate = nowDay + ":00:00:00";
        String edate = nowDay + ":23:59:59";
        Admin admin = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int collegeid;
        if(admin.getCollegeid()!=null){
            collegeid = admin.getCollegeid();
        }else{
            collegeid=0;
        }

        return iSdakaService.getalldakaByCl(sdate,edate,collegeid);
    }

    @ApiOperation(value = "疫情地图数据")
    @GetMapping("/getMap")
    public String getMapData(){
        return HttpUtil.get("https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=" + System.currentTimeMillis());
    }


}
